package demo1;
import java.sql.Time;
import java.util.Comparator;
import java.util.TimerTask;
import java.util.concurrent.PriorityBlockingQueue;
class MyTimeTask implements Comparable<Integer> {//两个接口的区别
   private Runnable runnable;
   private int delay;
    public MyTimeTask(Runnable runnable, int time) {
        this.runnable = runnable;
        this.delay = (int) (System.currentTimeMillis()+time);
    }
    public void run(){
        runnable.run();
    }
    public int getDelay() {
        return delay;
    }
    /**
     * Compares this object with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     *
     * <p>The implementor must ensure {@link Integer#signum
     * signum}{@code (x.compareTo(y)) == -signum(y.compareTo(x))} for
     * all {@code x} and {@code y}.  (This implies that {@code
     * x.compareTo(y)} must throw an exception if and only if {@code
     * y.compareTo(x)} throws an exception.)
     *
     * <p>The implementor must also ensure that the relation is transitive:
     * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies
     * {@code x.compareTo(z) > 0}.
     *
     * <p>Finally, the implementor must ensure that {@code
     * x.compareTo(y)==0} implies that {@code signum(x.compareTo(z))
     * == signum(y.compareTo(z))}, for all {@code z}.
     *
     * @param o the object to be compared.
     * @return a negative integer, zero, or a positive integer as this object
     * is less than, equal to, or greater than the specified object.
     * @throws NullPointerException if the specified object is null
     * @throws ClassCastException   if the specified object's type prevents it
     *                              from being compared to this object.
     * @apiNote It is strongly recommended, but <i>not</i> strictly required that
     * {@code (x.compareTo(y)==0) == (x.equals(y))}.  Generally speaking, any
     * class that implements the {@code Comparable} interface and violates
     * this condition should clearly indicate this fact.  The recommended
     * language is "Note: this class has a natural ordering that is
     * inconsistent with equals."
     */
    @Override
    public int compareTo(Integer o) {
        return this.delay-o;
    }
    /**
     * Compares its two arguments for order.  Returns a negative integer,
     * zero, or a positive integer as the first argument is less than, equal
     * to, or greater than the second.<p>
     * <p>
     * The implementor must ensure that {@link Integer#signum
     * signum}{@code (compare(x, y)) == -signum(compare(y, x))} for
     * all {@code x} and {@code y}.  (This implies that {@code
     * compare(x, y)} must throw an exception if and only if {@code
     * compare(y, x)} throws an exception.)<p>
     * <p>
     * The implementor must also ensure that the relation is transitive:
     * {@code ((compare(x, y)>0) && (compare(y, z)>0))} implies
     * {@code compare(x, z)>0}.<p>
     * <p>
     * Finally, the implementor must ensure that {@code compare(x,
     * y)==0} implies that {@code signum(compare(x,
     * z))==signum(compare(y, z))} for all {@code z}.
     *
     * @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the
     * first argument is less than, equal to, or greater than the
     * second.
     * @throws NullPointerException if an argument is null and this
     *                              comparator does not permit null arguments
     * @throws ClassCastException   if the arguments' types prevent them from
     *                              being compared by this comparator.
     * @apiNote It is generally the case, but <i>not</i> strictly required that
     * {@code (compare(x, y)==0) == (x.equals(y))}.  Generally speaking,
     * any comparator that violates this condition should clearly indicate
     * this fact.  The recommended language is "Note: this comparator
     * imposes orderings that are inconsistent with equals."
     */
    }
public class MyTimerPlus {
    //模拟带定时器的线程池
//    1、核心线程数，最大线程数
//    2、任务队列BlockingQueue
//    3、计时器
//    4、提交方法
    //任务队列：由于有个定时需求，所以采取优先级队列，小堆顶,但是优先级队列自带一把锁
    private PriorityBlockingQueue<MyTimeTask> queue=new PriorityBlockingQueue<>(1000);
    private int MaxThread;
    private int currentThread;
    public MyTimerPlus() {
        //MaxThread = maxThread;
        //currentThread=coreNum;
            Thread t=new Thread(new TimerTask() {
                @Override
                public void run() {
                    while (true){
                        MyTimeTask myTimeTask=queue.peek();
                        if(myTimeTask.getDelay()>=System.currentTimeMillis()){
                            myTimeTask.run();
                            queue.poll();
                        }
                        continue;
                    }
                }
            });
        }
        public void schedule(MyTimeTask myTimeTask){
        queue.put(myTimeTask);
        }
    }

